package com.anysoft.ui.activity;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.anysoft.R;
import com.anysoft.base.BaseBaseActivity;
import com.anysoft.base.BasePresenter;
import com.anysoft.base.BaseWebViewFragment;
import com.anysoft.manager.UserInfoManager;
import com.anysoft.ui.activity.web.TouristsActivity;
import com.anysoft.ui.fragment.ParentForwardFragment;
import com.anysoft.ui.fragment.ParentHomeFragment;
import com.anysoft.utils.ActivityCollector;
import com.anysoft.widget.BottomNavigationViewEx;

import java.util.ArrayList;

import butterknife.BindView;


/**
 * Created by lty on 2018/7/2.
 */
public class ParentHomeActivity extends BaseBaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    
    @BindView(R.id.fl_home_container)
    FrameLayout fl_home_container;
    @BindView(R.id.navigation_view)
    BottomNavigationViewEx navigation_view;
    private ArrayList<BaseWebViewFragment> mFragments;
    private int mLastFgIndex;
    private ParentHomeFragment parentTab;
    private ParentForwardFragment forwardTab;
    
    @Override
    protected void doBeforeSetContentView() {
        super.doBeforeSetContentView();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        if (UserInfoManager.getInstance(this).needLogin()) {
            ActivityCollector.startTargetActivity(this, TouristsActivity.class);
            finish();
        }
        switchFragment(mLastFgIndex);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_home_layout;
    }
    
    @Override
    public void initView() {
        super.initView();
        navigation_view.enableAnimation(false);
        navigation_view.enableItemShiftingMode(false);
        navigation_view.enableShiftingMode(false);
        navigation_view.setOnNavigationItemSelectedListener(this);
        
    }
    
    @Override
    public void initData() {
        mFragments = new ArrayList<>();
        parentTab = ParentHomeFragment.getInstance();
        forwardTab = ParentForwardFragment.getInstance();
        mFragments.add(parentTab);
        mFragments.add(forwardTab);
        MenuItem homeItem = navigation_view.getMenu().getItem(0);
        homeItem.setTitle("首页");
        homeItem.setIcon(R.drawable.nav_item_home);
        MenuItem childItem = navigation_view.getMenu().getItem(1);
        childItem.setTitle("家长端");
        childItem.setIcon(R.drawable.nav_item_child);
        childItem.setChecked(true);
        MenuItem settingItem = navigation_view.getMenu().getItem(2);
        settingItem.setTitle("我的预约");
        settingItem.setIcon(R.drawable.nav_item_schedule);
    }
    
    @Override
    public void initListener() {
        navigation_view.setOnNavigationItemSelectedListener(this);
    }
    
    /**
     * 切换fragment
     *
     * @param position 要显示的fragment的下标
     */
    private void switchFragment(int position) {
        if (mFragments == null) {
            return;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment targetFg = mFragments.get(position);
        reloadPage(position);
        Fragment lastFg = mFragments.get(mLastFgIndex);
        mLastFgIndex = position;
        ft.hide(lastFg);
        if (!targetFg.isAdded()) {
            getSupportFragmentManager().beginTransaction().remove(targetFg).commit();
            ft.add(R.id.fl_home_container, targetFg);
        }
        ft.show(targetFg);
        ft.commitAllowingStateLoss();
    }
    
    private void reloadPage(int position) {
        BaseWebViewFragment baseFragment = mFragments.get(position);
        switch (position) {
            case 0:
                ((ParentHomeFragment) baseFragment).reloadPage();
                break;
            case 1:
                ((ParentForwardFragment) baseFragment).reloadPage();
                break;
        }
    }
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.tab_home:
                ActivityCollector.startTargetActivity(this, TouristsActivity.class);
                break;
            case R.id.tab_parent:
                switchFragment(0);
                break;
            case R.id.tab_forward:
                switchFragment(1);
                break;
        }
        return true;
    }
}
